#!/usr/bin/env python3
"""添加华为设备到备份系统"""

import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from sqlalchemy.orm import Session
from backend.database import get_db
from backend.models import Device

def add_huawei_device():
    """添加华为设备到数据库"""
    db = next(get_db())
    
    # 华为设备配置（基于测试脚本中的参数）
    huawei_device = Device(
        name="出口路由器",
        hostname="192.168.30.100",
        ip_address="192.168.30.100",
        device_type="huawei_vrp",  # 使用 huawei_vrp 类型
        port=22,
        username="test",
        password="123456789",
        enable_password=None,  # 华为设备通常不需要enable密码
        description="华为出口路由器 - 测试设备",
        is_active=True,
        auto_backup=True,
        backup_interval=24  # 24小时备份一次
    )
    
    try:
        # 检查设备是否已存在
        existing_device = db.query(Device).filter(Device.name == huawei_device.name).first()
        if existing_device:
            print(f"设备 '{huawei_device.name}' 已存在，ID: {existing_device.id}")
            print("是否要更新设备信息？(y/n): ", end="")
            choice = input().lower()
            if choice == 'y':
                # 更新现有设备
                existing_device.hostname = huawei_device.hostname
                existing_device.ip_address = huawei_device.ip_address
                existing_device.device_type = huawei_device.device_type
                existing_device.port = huawei_device.port
                existing_device.username = huawei_device.username
                existing_device.password = huawei_device.password
                existing_device.enable_password = huawei_device.enable_password
                existing_device.description = huawei_device.description
                existing_device.is_active = huawei_device.is_active
                existing_device.auto_backup = huawei_device.auto_backup
                existing_device.backup_interval = huawei_device.backup_interval
                
                db.commit()
                print(f"✓ 设备 '{huawei_device.name}' 信息已更新")
                return existing_device
            else:
                print("操作已取消")
                return existing_device
        
        # 添加新设备
        db.add(huawei_device)
        db.commit()
        db.refresh(huawei_device)
        
        print("=" * 60)
        print("华为设备添加成功！")
        print("=" * 60)
        print(f"设备ID: {huawei_device.id}")
        print(f"设备名称: {huawei_device.name}")
        print(f"IP地址: {huawei_device.ip_address}")
        print(f"设备类型: {huawei_device.device_type}")
        print(f"用户名: {huawei_device.username}")
        print(f"端口: {huawei_device.port}")
        print(f"自动备份: {'是' if huawei_device.auto_backup else '否'}")
        print(f"备份间隔: {huawei_device.backup_interval} 小时")
        print(f"描述: {huawei_device.description}")
        print("=" * 60)
        
        return huawei_device
        
    except Exception as e:
        db.rollback()
        print(f"✗ 添加设备失败: {e}")
        return None
    finally:
        db.close()

if __name__ == "__main__":
    print("华为设备集成脚本")
    print("=" * 60)
    print("正在添加华为设备到备份系统...")
    
    device = add_huawei_device()
    
    if device:
        print("\n下一步操作建议:")
        print("1. 运行 'python backend/main.py' 启动后端服务")
        print("2. 访问前端界面测试设备连接")
        print("3. 尝试手动备份设备配置")
        print("4. 检查备份文件是否生成正确")
    else:
        print("\n设备添加失败，请检查错误信息")
